package com.cxh.rmsq.utils.dialog

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.blankj.utilcode.util.ToastUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.utils.TextSpanUtil
import com.hjq.shape.view.ShapeEditText
import com.hjq.shape.view.ShapeTextView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.xuexiang.xui.utils.XToastUtils
import kotlin.math.roundToInt


class PayPopup(
    context: Context,
    content: String,
    flag: String,
    click: onClick
) :
    BottomPopupView(context) {

    interface onClick {
        fun click(content: String, s1: Int)
    }

    var func: onClick
    var content: String
    var flag: String
    var s1: Int = 1

    init {
        this.func = click
        this.content = content
        this.flag = flag
    }

    override fun getImplLayoutId(): Int {
        return R.layout.pay_popup
    }

    override fun onCreate() {
        super.onCreate()

        var iv_del = findViewById<ImageView>(R.id.iv_del)
        var tv_price = findViewById<TextView>(R.id.tv_price)
        var stv_pay = findViewById<ShapeTextView>(R.id.stv_pay)
        var checkbox1 = findViewById<CheckBox>(R.id.checkbox1)
        var view2 = findViewById<View>(R.id.view2)
        var view4 = findViewById<View>(R.id.view4)
        var checkbox2 = findViewById<CheckBox>(R.id.checkbox2)
        var checkbox3 = findViewById<CheckBox>(R.id.checkbox3)
        var checkbox4 = findViewById<CheckBox>(R.id.checkbox4)
        var ll_pay_wechat = findViewById<LinearLayout>(R.id.ll_pay_wechat)
        var ll_pay_wechat_qrcode = findViewById<LinearLayout>(R.id.ll_pay_wechat_qrcode)
        var ll_pay_alipay = findViewById<LinearLayout>(R.id.ll_pay_alipay)
        var ll_pay_alipay_qrcode = findViewById<LinearLayout>(R.id.ll_pay_alipay_qrcode)
        tv_price.setText(content)

        if (!TextUtils.isEmpty(flag)){//VIP
            ll_pay_wechat_qrcode.isVisible = true
            view2.isVisible = true
            ll_pay_alipay_qrcode.isVisible = true
            view4.isVisible = true
        }else{
            ll_pay_wechat_qrcode.isVisible = false
            view2.isVisible = false
            ll_pay_alipay_qrcode.isVisible = false
            view4.isVisible = false
        }
        iv_del.setOnClickListener {
            dismiss()
        }
        ll_pay_wechat.setOnClickListener {
            checkbox1.isChecked = true
            checkbox2.isChecked = false
            checkbox3.isChecked = false
            checkbox4.isChecked = false
            s1 = 1
        }
        ll_pay_wechat_qrcode.setOnClickListener {
            checkbox1.isChecked = false
            checkbox2.isChecked = true
            checkbox3.isChecked = false
            checkbox4.isChecked = false
            s1 = 4
        }
        ll_pay_alipay.setOnClickListener {
            checkbox1.isChecked = false
            checkbox2.isChecked = false
            checkbox4.isChecked = false
            checkbox3.isChecked = true
            s1 = 2
        }
        ll_pay_alipay_qrcode.setOnClickListener {
            checkbox1.isChecked = false
            checkbox2.isChecked = false
            checkbox3.isChecked = false
            checkbox4.isChecked = true
            s1 = 5
        }
        stv_pay.setOnClickListener {
            func.click(
                tv_price.text.toString().trim(),
                s1,
            )
            dismiss()
        }

    }

    // 最大高度为Window的0.85
    override fun getMaxHeight(): Int {
        return ((XPopupUtils.getAppHeight(context) * .55f).roundToInt())
    }

}